package com.renli.service.impl;

import com.renli.bean.ResultJson;
import com.renli.mapper.QuestionMapper;
import com.renli.pojo.Question;
import com.renli.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public ResultJson<List<Question>> find(Map<String, Object> map) {
        /*
        *  "id：明细id" +
            "currentPage：当前页（默认1）" +
            "pageSize：页面大小（默认6）" +
            "title：试题标题" +
            "questionTypeId：试题类型id" +
            "depId：所属部门id" +
            "state：是否启用")*/
        if (map == null) {
            map = new HashMap<>();
        }
        if (map.get("currentPage") == null || map.get("currentPage").equals("")) {
            map.put("currentPage", 1);
        }
        if (map.get("pageSize") == null || map.get("pageSize").equals("")) {
            map.put("pageSize", 6);
        }

        Integer start = (Integer) map.get("pageSize") * ((Integer) map.get("currentPage") - 1);
        map.put("start", start);

        ResultJson<List<Question>> resultJson = new ResultJson<>();

        Long count = questionMapper.findAllCount(map);

        List<Question> list = questionMapper.findAll(map);

        resultJson.setSuccess("查询成功", list, count);

        return resultJson;
    }

    /**
     * 文件待完成
     *
     * @param question
     * @return
     */
    @Override
    public ResultJson add(Question question) {


        if (questionMapper.add(question) == 1) {
            return new ResultJson().setSuccess("新增成功");
        } else {
            return new ResultJson().setSuccess("新增失败");
        }

    }

    @Override
    public ResultJson del(Integer id) {
        if (questionMapper.delById(id) == 1) {
            return new ResultJson().setSuccess("删除成功");
        } else {
            return new ResultJson().setSuccess("删除失败");
        }

    }


    @Override
    public ResultJson update(Question question) {
        if (questionMapper.update(question)==1){
            return new ResultJson().setSuccess("修改成功");
        }else {
            return new ResultJson().setSuccess("修改失败");
        }
    }

}
